// subPackages/packageA/shesuList/shesuList.ts
import { getSxListApi, getSxListDropdownApi, getSxYearCountListApi } from "../../api/shesu"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    container: null,
    page: 1,
    perPage: 10,
    debtorId: "",
    companyName: "",
    total: 0,
    loading: false,
    query: "",
    searchInputClass: "",
    placeholderText: "搜索...",
    //最小案由
    nLaayTreeMiniValue: "",
    nLaayTreeMini: {
      options: [
        {
          value: '',
          label: '全部'
        },
      ],
    },
    // 年份
    yearInfoValue: "",
    yearInfo: {
      options: [
        {
          value: '',
          label: '全部'
        },
      ],
    },
    // 文书详情
    wxStatusValue: "",
    wxStatus: {
      options: [
        {
          value: '',
          label: '全部'
        },
        {
          value: '1',
          label: '有',
        },
        {
          value: '0',
          label: '无',
        }
      ]
    },
    contentList: [],
  },
  // search-bar 搜索栏
  onChange(e: any) {
    const id = e.currentTarget.id
    const value = e.detail.value
    if (id === '1') {
      this.setData({
        "nLaayTreeMiniValue": value
      });
    }
    if (id === '2') {
      this.setData({
        "yearInfoValue": value
      });
    }
    if (id === '3') {
      this.setData({
        "wxStatusValue": value
      });
    }
    this.searchHandle();
  },
  searchHandle() {
    this.data.page = 1;
    this.setData({
      contentList: []
    })
    this.getList();
  },
  // 点击展开搜索
  searchFocus() {
    this.setData({
      searchInputClass: "input-is-focus",
      placeholderText: this.data.query || "输入企业名称,案号,经办法院"
    }, () => {
      this.setData({
        searchDisabled: false,
        clearable: true
      })
    })
  },
  // 确认搜索
  searchBlur() {
    this.searchHandle();
  },
  // 收起
  searchFold() {
    this.setData({
      searchInputClass: "",
      placeholderText: "搜索...",
      searchDisabled: true,
      clearable: false
    })
  },
  // 上拉到底
  scrolltolower() {
    if (this.data.total > this.data.contentList.length) {
      this.data.page++;
      this.getList();
    }
  },
  // 下拉刷新
  refresherrefresh() {
    this.searchHandle();
  },
  // 查看涉诉详情
  viewDetail(e: any) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/subPackages/packageA/shesu/shesuDetail/shesuDetail?id=" + id
    })
  },
  // 查看文书
  checkWs(e: any) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/subPackages/packageA/writDetail/writDetail?id=" + id
    })
  },
  // 获取最小案由统计
  getMiniCaseOptions() {
    getSxListDropdownApi({ name: this.data.companyName }).then((res: any) => {
      let content = res.content.map((item: any) => {
        item.label = item.name + `(${item.value})`;
        item.value = item.name;
        return item;
      })
      content.unshift({
        value: "",
        label: "全部"
      })
      this.setData({
        "nLaayTreeMini.options": content
      })
    })
  },
  // 获取年份统计
  getYearOptions() {
    getSxYearCountListApi({ id: this.data.debtorId }).then((res: any) => {
      let content = res.content.map((item: any) => {
        item.label = item.name + `(${item.value})`;
        item.value = item.name;
        return item;
      })
      content.unshift({
        value: "",
        label: "全部"
      })
      this.setData({
        "yearInfo.options": content
      })
    })
  },
  // 获取涉诉信息列表
  getList() {
    this.setData({
      loading: true
    })
    let para = {
      page: this.data.page,
      perPage: this.data.perPage,
      id: this.data.debtorId,
      query: this.data.query,
      nLaayTreeMini: this.data.nLaayTreeMiniValue,
      yearInfo: this.data.yearInfoValue,
      wxStatus: this.data.wxStatusValue,
    }

    getSxListApi(para).then((res: any) => {
      let contentList = res.content.list;
      this.data.total = res.content.total;
      this.setData({
        loading: false,
        contentList: this.data.contentList.concat(contentList),
        total: res.content.total
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option: any) {
    // console.log(option, "option")
    this.data.debtorId = option.debtorId;
    this.data.companyName = option.companyName;
    this.setData({
      debtorId: this.data.debtorId,
      companyName: this.data.companyName
    })
    this.getList();
    this.getMiniCaseOptions();
    this.getYearOptions();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },


})